// pages/add-share/index.js
var api = require('../../api.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      imgServer :  api.default.img_server,
      form:{
      name:'',
      mobile:'',
    },
    myForm:{},
    img: "/images/img-share-un.png",
    agree: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var page = this;
    var user_info = wx.getStorageSync("user_info");
    var store = wx.getStorageSync("store");
    var share_setting = wx.getStorageSync("share_setting");
    page.setData({
      user_info: user_info,
      store:store,
      share_setting: share_setting
    });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */ 
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  inputedit1: function (e) {
    this.data.myForm.business_name = e.detail.value;
  },
  inputedit2: function (e) {
    this.data.myForm.longitude = e.detail.value;
  },
  inputedit3: function (e) {
    this.data.myForm.address = e.detail.value;
  },
  inputedit4: function (e) {
    this.data.myForm.latitude = e.detail.value;
  },
  inputedit5: function (e) {
    this.data.myForm.city = e.detail.value;
  },
  inputedit6: function (e) {
    this.data.myForm.business_detail = e.detail.value;
  },
  inputedit7: function (e) {
    this.data.myForm.province = e.detail.value;
  },
  inputedit8: function (e) {
    this.data.myForm.business_license = e.detail.value;
  },
  inputedit9: function (e) {
    this.data.myForm.business_scope = e.detail.value;
  },
  inputedit10: function (e) {
    this.data.myForm.region = e.detail.value;
  },
  inputedit11: function (e) {
    this.data.myForm.reference_name = e.detail.value;
  },
  inputedit12: function (e) {
    this.data.myForm.reference_phone = e.detail.value;
  },
  inputedit13: function (e) {
    this.data.myForm.remarks = e.detail.value;
  },

  orderSubmit: function (e) {
    var page = this;
    wx.requestSubscribeMessage({
      tmplIds: ['2Ni3pxgwXhZ-BgC9UgPU-lN-pC5c_95CCsUzHFTFlOo'],
      success(res) {
          page.formSubmit(e);
      }
    })
},

  formSubmit: function (e) {
    var page = this;
    var user_info = wx.getStorageSync("user_info");
    var my_info = wx.getStorageSync("my_info");
    console.log(my_info)

    page.data.form =page.data.myForm;
    console.log(page.data.form)
    if (this.isBlank(page.data.form.business_name)) {
      return;
    }
    if (this.isBlank(page.data.form.longitude)) {
      return;
    }
    if (this.isBlank(page.data.form.latitude)) {
      return;
    }
    if (this.isBlank(page.data.form.city)) {
      return;
    }
    if (this.isBlank(page.data.form.business_detail)) {
      return;
    }
    if (this.isBlank(page.data.form.address)) {
      return;
    }else{
      if (!this.isPhone(page.data.form.address)){
        wx.showModal({
          content: "手机号码格式有误",
          showCancel: false,
        })
        return
      }
    }
    
    if (this.isBlank(page.data.form.province)) {
      return;
    }

    if (page.data.form.reference_phone == undefined || page.data.form.reference_phone == '') {
    } else {
      if (!this.isPhone(page.data.form.reference_phone)) {
        wx.showModal({
          content: "推荐人手机号码格式有误",
          showCancel: false,
        })
        return
      }
    }

 
  
    console.log(page.data.agree);
    if (page.data.agree == 0) {
      wx.showModal({
        content: "请先阅读并确认供应商申请协议！！",
        showCancel: false,
      })
      return ;
    }
    console.log(page.data.agree);
    var test = page.data.myForm;
    test.wxId = wx.getStorageSync("access_token")

    app.request({
      url: api.share.join,
      method:'POST',
      data: test,
      success: function (res) {
        console.log(res)
        if (res.code == 200) {
          user_info.is_distributor = 2;
          wx.setStorageSync(
            "user_info",user_info
          );
          // app.login();
          wx.login({
            success: function (res) {
              if (res.code) {
                var code = res.code;
                console.log(res)
                getApp().loginrequest({
                  url: api.passport.quick_login,
                  method: "post",
                  data: {
                    code: code
                  },
                  success: function (res) {
                    console.log("quickLoginSuccessful")
                    console.log(res)
                    wx.hideLoading();
                    if (res.code == 200) {
                      const tokenStr = res.data.tokenHead + res.data.token
                      wx.setStorageSync('loginToken', tokenStr)
                      wx.setStorageSync('token', res.data.token);
                      wx.setStorageSync("access_token", res.data.userId);
                      wx.setStorageSync("user_info", {
                        avatar_url: res.data.img,
                        nickname: res.data.username,
                        avatar_url: res.data.icon,
                        // parent: res.data.parent,
                        id: res.data.id
                      });
                      wx.setStorageSync("my_info", {
                        supplyId: res.data.myInfo.supplyId,
                        supplyState: res.data.myInfo.supplyState,
                        myMoney: res.data.myInfo.myMoney,
                        icon: res.data.myInfo.icon,
                        nickname: res.data.myInfo.nickname,
                        memberLevelId: res.data.myInfo.memberLevelId,
                        id: res.data.myInfo.id,
                        city: res.data.myInfo.city,
                        gender: res.data.myInfo.gender,
                        job: res.data.myInfo.job,
                        phone: res.data.myInfo.phone,
                        birthday: res.data.myInfo.birthday,
                        // parent: res.data.parent,
                      });

                      wx.showModal({
                        title: '',
                        content: '您的申请已经提交成功！稍后，平台客服会添加供应商申请表中的微信号告知审核结果！',
                        showCancel: false,//是否显示取消按钮
                        confirmText: "确定",//默认是“确定”
                        success: function (res) {
                          if (res.cancel) {
                            //点击取消,默认隐藏弹框
                          } else {
                            //点击确定
                            wx.navigateBack({
                              delta: 1
                            })
                          }
                        },
                        fail: function (res) { },//接口调用失败的回调函数
                        complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
                      })



                    }
                  }
                });
              }


            }
          });





         
     
        } else {
          wx.showModal({
            content: res.msg,
            showCancel: false,
          })
          // wx.showToast({
          //   title: res.msg,
          //   image: "/images/icon-warning.png",
          // });
        }
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  agreement: function () {
    wx.redirectTo({
      url: '/pages/xieyi/xieyi',
    })
    // var share_setting = wx.getStorageSync("share_setting");
    // wx.showModal({
    //   title: '供应商协议',
    //   content: "sdfzzfdfzfrfffg",
    //   showCancel:false,
    //   confirmText:"我已阅读",
    //   confirmColor:"#ff4544",
    //   success: function (res) {
    //     if (res.confirm) {
    //       console.log('用户点击确定')
    //     }
    //   }
    // });
  },
  agree:function(){
    var page = this;
    var agree = page.data.agree;
    if (agree == 0) {
      agree = 1;
      page.setData({
        img: "/images/img-share-agree.png",
        agree:agree
      });
    }
    else if (agree == 1) {
      agree = 0;
      page.setData({
        img: "/images/img-share-un.png",
        agree: agree
      });
    }
  },

  chooseImage: function (e) {
    var that = this
    var type = e.currentTarget.dataset.id
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        if (type == 1){
          that.setData({
            license: tempFilePaths[0]
          })
        }else{
          that.setData({
            cert: tempFilePaths[0]
          })
        }
        that.uploadFileImage(tempFilePaths[0])
      }
    })
  },
  /**
   * 图片上传api调用
   */
  uploadFileImage(img) {
    wx.uploadFile({
      url: api.default.upload_image,//"/api/aliyun/oss/callback",
      filePath: img,
      name: 'file',
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res
      }
    })
  },
  // 手机验证
  isPhone: function (phone) {
    if (!(/^1[2345789]\d{9}$/.test(phone))) {
      if (phone.length >= 11) {
        return false
      }
    } else {
      return true
    }
  },
  isBlank:function(text){
    if (text == undefined || text == ''){
      wx.showModal({
        content: "有必填项未填写!",
        showCancel: false,
      })
      return true
    }
  }
})